import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.BorderFactory;
import javax.swing.JPanel;



public class ClockPanel extends JPanel
{
	/**
	 * 
	 */
	private ClockGeometry geom;
	private boolean visible = true;
	public static Dimension panelDimension = new Dimension(200, 200);
	private static final long serialVersionUID = 1L;
	
	public ClockPanel(ClockGeometry geom)
	{
		this.geom = geom;
		setBorder(BorderFactory.createLineBorder(Color.black));
	}
	
	public Dimension getPreferredSize() 
	{
        return new Dimension(ClockPanel.panelDimension);
    }
	
	protected void paintComponent(Graphics g) 
	{
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D)g;
        if(visible == false)
        {
        	g2d.clearRect(0, 0, getPreferredSize().width, getPreferredSize().height);
        	return;
        }
        
        WorldToPanelCoordinates converter = new WorldToPanelCoordinates(ClockPanel.panelDimension);
        Point clockCenterPos = new Point(geom.clockCenterPos);
        Point hourHandsPos = new Point(geom.getHourHandPos());
        Point minutesHandsPos = new Point(geom.getMinutesHandPos());
        // convert world to panel coordinates
        converter.worldToPanel(clockCenterPos);
        converter.worldToPanel(hourHandsPos);
        converter.worldToPanel(minutesHandsPos);
        
        // draw hour hand
        g2d.setStroke(new BasicStroke(4F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2d.drawLine(geom.clockCenterPos.x, geom.clockCenterPos.y, hourHandsPos.x, hourHandsPos.y);
        // draw clock
        g2d.setStroke(new BasicStroke(2F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2d.drawOval(clockCenterPos.x -  geom.clockRadius, clockCenterPos.y - geom.clockRadius, geom.clockRadius * 2, geom.clockRadius * 2);
        // draw minute hand
        g2d.drawLine(geom.clockCenterPos.x, geom.clockCenterPos.y, minutesHandsPos.x, minutesHandsPos.y);
        // draw hour text
        g2d.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
        for(int i = 1; i < 13; ++i)
        {
        	Point textPos = new Point( geom.getHoursTextPos(i));
        	 converter.worldToPanel(textPos);
        	g2d.drawString(geom.hourTextArray[i-1], textPos.x - 5, textPos.y + 5);
        }
    }
	
	public void setHours(int hour)
	{
		geom.setHour(hour);
		repaint();
	}
	
	public void setMinutes(int min)
	{
		geom.setMinutes(min);
		repaint();
	}
}
